<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改菜单列')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-menu-edit" th:object="${wxMenu}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">菜单名称：</label>
                <div class="col-sm-8">
                    <input name="menuName" th:field="*{menuName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">菜品图片：</label>
                <div class="col-sm-8">
                    <input  type="file" name="file" id="file0"   multiple="multiple"/>
                    <img th:src="*{menuPictureUrl}" id="img0" style="width: 200px;height: 150px;" >
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">描述：</label>
                <div class="col-sm-8">
                    <input name="menuDescription" th:field="*{menuDescription}" class="form-control" type="text">
                </div>
            </div>


        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script type="text/javascript">
         var prefix = ctx + "wechatprogram/menu";
        // $("#form-menu-edit").validate({
        //     focusCleanup: true
        // });
        //
        // function submitHandler() {
        //     if ($.validate.form()) {
        //         $.operate.save(prefix + "/edit", $('#form-menu-edit').serialize());
        //     }
        // }

        $("#file0").change(function(){
            var objUrl = getObjectURL(this.files[0]) ;
            objUrl = getObjectURL(this.files[0]) ;
            console.log("objUrl = "+objUrl) ;
            if (objUrl) {
                $("#img0").attr("src", objUrl) ;
            }
        }) ;
        //建立一個可存取到該file的url
        function getObjectURL(file) {
            var url = null ;
            if (window.createObjectURL!=undefined) { // basic
                url = window.createObjectURL(file) ;
            } else if (window.URL!=undefined) { // mozilla(firefox)
                url = window.URL.createObjectURL(file) ;
            } else if (window.webkitURL!=undefined) { // webkit or chrome
                url = window.webkitURL.createObjectURL(file) ;
            }
            return url ;
        }

        function submitHandler() {
            var menuId= $("input[name=id]").val();
            var menuName = $("input[name=menuName]").val();
            var file = document.getElementById('file0').files[0];
            var menuDescription = $("input[name=menuDescription]").val();
            var formdata = new FormData();
            formdata.append("menuId", menuId);
            formdata.append("menuName", menuName);
            formdata.append("file", file);
            formdata.append("menuDescription", menuDescription);
            $.ajax({
                url: prefix + "/edit",
                data: formdata,
                type: "post",
                processData: false,
                contentType: false,
                success: function(result) {
                    $.operate.saveSuccess(result);
                }
            })
        }

    </script>
</body>
</html>